package com.yk.faceapplication.http;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.yk.faceapplication.util.LogDebugger;

public class VolleyStringUtil extends Request<String> {

	private Map<String, String> mMap;
	private Listener<String> mListener;

	public VolleyStringUtil(String url, Listener<String> listener,
			ErrorListener errorListener, Map<String, String> map) {
		super(Request.Method.POST, url, errorListener);
		mListener = listener;
		mMap = map;
	}

	@Override
	protected Map<String, String> getParams() throws AuthFailureError {

		return mMap;
	}

	@Override
	protected Response<String> parseNetworkResponse(NetworkResponse response) {
		try {
			String jsonString = new String(response.data,
					HttpHeaderParser.parseCharset(response.headers));
			if(jsonString.equals("true")){
				jsonString = "{result:\"true\"}";
			}else if(jsonString.equals("false")){
				jsonString = "{result:\"false\"}";
			}
			LogDebugger.debug("jsonString:"+jsonString);
			
			return Response.success(jsonString,HttpHeaderParser.parseCacheHeaders(response));
		} catch (UnsupportedEncodingException e) {
			return Response.error(new ParseError(e));
		}
	}
            
	@Override
	protected void deliverResponse(String response) {
		mListener.onResponse(response);

	}
}
